import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  // @ts-ignore
  history: createWebHashHistory(import.meta.env.BASE_URL),
  mode: "hash",
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../views/home/index.vue"),
    },
    {
      path: "/about",
      name: "about",
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/index.vue"),
    },
    {
      path: "/singin",
      name: "singin",
      component: () => import("../views/singin/index.vue"),
    },
    {
      path: "/forget",
      name: "forget",
      component: () => import("../views/forget/index.vue"),
    },
    {
      path: "/search",
      name: "search",
      component: () => import("../views/product/search.vue"),
    },
    // 用户模块
    {
      path: "/user",
      name: "user",
      component: () => import("../views/user/index.vue"),
      children: [
        {
          path: "info",
          name: "user-info",
          meta: { requiresAuth: true },
          component: () => import("../views/user/info/index.vue"),
        },
        {
          path: "gift",
          name: "user-gift",
          component: () => import("../views/user/gift/index.vue"),
        },
        {
          path: "gift/exchange/:id",
          name: "user-gift-exchange",
          meta: { activePath: "/user/gift" ,requiresAuth: true},
          component: () => import("../views/user/gift/exchange.vue"),
        },
        {
          path: "gift/order",
          name: "user-gift-order",
          meta: { activePath: "/user/gift/order" ,requiresAuth: true},
          component: () => import("../views/user/gift/order.vue"),
        },
        {
          //订单列表
          path: "order",
          name: "user-order",
          meta: { requiresAuth: true },
          component: () => import("../views/user/order/index.vue"),
        },
        {
          //订单详情
          path: "order/:id",
          name: "user-order-deatil",
          meta: { activePath: "/user/order", requiresAuth: true },
          component: () => import("../views/user/order/deatil.vue"),
        },
      ],
    },
  ],
});

function checkAuth() {
  const token = localStorage.getItem("user-token"); // 假设你将 token 存储在 localStorage 中

  if (token) {
    // 在实际项目中，你可能需要将 token 发送到后端验证，这里假设验证通过
    return true;
  } else {
    return false;
  }
}

router.beforeEach((to, from, next) => {
  const isAuthenticated = checkAuth(); // 检查用户是否已登录，可以根据实际情况实现该函数

  if (
    to.matched.some((record) => record.meta.requiresAuth) &&
    !isAuthenticated
  ) {
    // 如果用户未登录且访问需要登录的页面，则重定向到登录页面
    // @ts-ignore
    ElMessage.warning("请先登录"); 
    next("/login");
  } else {
    next();
  }
});
export default router;
